package com.android.gufengmall.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.android.gufengmall.R;
import com.android.gufengmall.adapter.CarouselAdapter;
import com.android.gufengmall.adapter.CarouselTitleAdapter;
import com.android.gufengmall.bean.CarouselBean;
import com.android.gufengmall.utils.LogUtils;
import com.android.gufengmall.utils.TabLayoutMediator;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.config.BannerConfig;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.util.BannerUtils;

public class HomeFragment extends Fragment {

    private static final String TAG = "HomeFragment";
    private Banner mBanner;
    private CarouselAdapter mCarouselAdapter;
    private ViewPager2 mViewPager2;
    private TabLayout mTabLayout;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        initView(root);
        initData();
        initListener();
        return root;
    }

    private void initData() {
        //自定义的图片适配器，也可以使用默认的BannerImageAdapter
        mCarouselAdapter = new CarouselAdapter(CarouselBean.getCarouselData());
        mBanner.setAdapter(mCarouselAdapter)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(requireContext()))//设置指示器
                .setOnBannerListener((data, position) -> {
                    Snackbar.make(mBanner, ((CarouselBean) data).title, Snackbar.LENGTH_SHORT).show();
                    LogUtils.d("position：" + position);
                });

        mBanner.setAdapter(new CarouselTitleAdapter(CarouselBean.getCarouselData()));
        mBanner.setIndicator(new CircleIndicator(requireContext()));
        mBanner.setIndicatorGravity(IndicatorConfig.Direction.RIGHT);
        mBanner.setIndicatorMargins(new IndicatorConfig.Margins(0, 0,
                BannerConfig.INDICATOR_MARGIN, BannerUtils.dp2px(12)));

        mViewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                if (position == 0) {
                    return literatureFragment.newInstance();
                } else if (position == 1) {
                    return ApparelFragment.newInstance();
                } else if (position == 2) {
                    return FolkCraftFragment.newInstance();
                } else {
                    return TalentFragment.newInstance();
                }
            }

            @Override
            public int getItemCount() {
                return 4;
            }
        });

        new TabLayoutMediator(mTabLayout, mViewPager2, (tab, position) -> {
            LogUtils.d(TAG, "position-->" + position);
            if (position == 0) {
                tab.setText("文学");
            } else if (position == 1) {
                tab.setText("服饰");
            } else if (position == 2) {
                tab.setText("民间工艺");
            } else {
                tab.setText("琴棋书画");
            }
        }).attach();
    }

    private void initListener() {

    }

    private void initView(View root) {
        mBanner = root.findViewById(R.id.banner);
        mViewPager2 = root.findViewById(R.id.vp2);
        mTabLayout = root.findViewById(R.id.tab_layout);
    }
}